package com.bw.zcb.mvp.view.user;

import android.graphics.Color;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bw.zcb.R;
import com.bw.zcb.mvp.base.model.BaseEntity;
import com.bw.zcb.mvp.base.view.BaseActivity;
import com.bw.zcb.mvp.contract.LoginContract;
import com.bw.zcb.mvp.di.DaggerLoginComponent;
import com.bw.zcb.mvp.di.LoginModules;
import com.bw.zcb.mvp.model.HttpCode;
import com.bw.zcb.mvp.model.entity.ResponseUserEntity;
import com.bw.zcb.mvp.model.entity.UserEntity;
import com.bw.zcb.mvp.presenter.LoginPresenter;
import com.bw.zcb.network.tool.App;
import com.bw.zcb.utils.ToastUtils;

public class RegisterActivity extends BaseActivity<LoginPresenter> implements LoginContract.ILoginView, View.OnClickListener {


    private android.widget.RelativeLayout login;
    private android.widget.RelativeLayout phoneMode;
    private android.widget.EditText editPhone;
    private android.widget.EditText editCode;
    private android.widget.Button loginCode;
    private android.widget.Button phoneLogin;
    private android.widget.TextView phoneModeCheckOne;
    private android.widget.RelativeLayout codeMode;
    private android.widget.EditText phoneName;
    private android.widget.EditText phonePwd;
    private android.widget.ImageView pwdVisibility;
    private android.widget.Button codeRegister;
    private android.widget.TextView phoneModeCheckTwo;
    private boolean flag = true;
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.code_register==id){
            if (phoneName.getText().toString().isEmpty()){
                ToastUtils.showMsg("手机号为空");
                return;
            }
            if (phonePwd.getText().toString().isEmpty()){
                ToastUtils.showMsg("密码为空");
                return;
            }
            p.requestLogin(Long.valueOf(phoneName.getText().toString()),phonePwd.getText().toString(), HttpCode.REGISTCODE);
            ToastUtils.showMsg("注册成功");
        }
        if (R.id.pwd_visibility==id){
            if (flag){
                flag=false;
                pwdVisibility.setImageResource(R.drawable.res_7icon_202);
                phonePwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//隐藏密码
            }else{
                flag=true;
                pwdVisibility.setImageResource(R.drawable.res_7icon_201);
                phonePwd.setTransformationMethod(PasswordTransformationMethod.getInstance());//显示密码
            }
            phonePwd.setSelection(phonePwd.getText().toString().length());
        }

    }

    @Override
    public View addStatusView() {
        return f(R.id.register);

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
        phoneMode = (RelativeLayout) findViewById(R.id.phone_mode);
        editPhone = (EditText) findViewById(R.id.edit_phone);
        editCode = (EditText) findViewById(R.id.edit_code);
        loginCode = (Button) findViewById(R.id.login_code);
        phoneLogin = (Button) findViewById(R.id.phone_login);
        codeRegister = findViewById(R.id.code_register);
        phoneModeCheckOne = (TextView) findViewById(R.id.phone_mode_check_one);
        codeMode = (RelativeLayout) findViewById(R.id.code_mode);
        phoneName = (EditText) findViewById(R.id.phone_name);
        phonePwd = (EditText) findViewById(R.id.phone_pwd);
        phoneModeCheckTwo = (TextView) findViewById(R.id.phone_mode_check_two);
        pwdVisibility = (ImageView) findViewById(R.id.pwd_visibility);
        codeRegister.setOnClickListener(this::onClick);
        phoneLogin.setOnClickListener(this::onClick);
        pwdVisibility.setOnClickListener(this::onClick);
        StatusView(Color.WHITE);

    }

    @Override
    public void initData() {

    }

    @Override
    public void inject() {
        DaggerLoginComponent.builder().loginModules(new LoginModules(this)).build().injectRegister(this);
    }

    @Override
    public void refreshView(BaseEntity baseEntity) {
        ResponseUserEntity entity = (ResponseUserEntity) baseEntity;
        if (entity.status.equals("200")) {
            UserEntity values = entity.getValues();
            int user_id = values.getUser_id();
            String user_img = values.getUser_img();
            int user_integral = values.getUser_integral();
            String user_name = values.getUser_name();
            int user_phone = values.getUser_phone();
            String user_pwd = values.getUser_pwd();
            String user_sex = values.getUser_sex();
            App.getInstance().getDaoSesson().insert(new UserEntity(user_id, user_phone, user_pwd, user_name, user_sex, user_img, user_integral));
            startPage(LoginActivity.class);
            finish();
        }
    }
}
